Class Money

java.lang.Object
  extended by Money
All Implemented Interfaces:
java.lang.Comparable

public class Money
extends java.lang.Object
implements java.lang.Comparable

An amount of money in Canadian currency; i.e., dollars and cents.

Version:
1.11, March 11, 2007, November 24, 2012, June 14, 2016
Author:
D.L. Bailey, Department of Systems and Computer Engineering, Carleton University, Lynn Marshall, Aly Moursy

Constructor Summary
Money(int cents)
          Constructs a Money object whose value is equal to the specified quantity of cents, which can be positive or negative, and < -99 or > 99.
Money(int dollars, int cents)
          Constructs a Money object whose value is equal to the specified quantity of dollars and cents, both of which can be positive or negative.
 
Method Summary
 Money addMonies(int number, int cents, java.lang.Object obj)
          A method to use for learning about exceptions.
 int compareTo(java.lang.Object obj)
          Compares this Money object with the specified Money object for order.
 Money dividedBy(int anAmount)
          Returns the result of dividing this Money object an integer amount.
 boolean equals(java.lang.Object obj)
          Compares this object against the specified object.
 boolean isEqualTo(Money anAmount)
          Compares this Money object against the specified Money object.
 Money minus(Money anAmount)
          Returns the difference of this Money object and the specified amount of money.
 Money multipliedBy(int anAmount)
          Returns the result of multiplying this Money object an integer amount.
 Money plus(Money anAmount)
          Returns the sum of this Money object and the specified amount of money.
 java.lang.String toString()
          Returns a String representation of this Money object in the form: "$ddd.cc" or "-$ddd.cc", where ddd is the dollars part and cc is the cents part.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Money

public Money(int cents)
Constructs a Money object whose value is equal to the specified quantity of cents, which can be positive or negative, and < -99 or > 99.

Parameters:
cents - A quantity of cents.
See Also:
Money(int, int)

Money

public Money(int dollars,
             int cents)
Constructs a Money object whose value is equal to the specified quantity of dollars and cents, both of which can be positive or negative. Cents must be between -99 and 99.

Parameters:
dollars - A quantity of dollars.
cents - A quantity of cents.
Throws:
java.lang.IllegalArgumentException - if cents outside valid range
See Also:
Money(int)
Method Detail

addMonies

public Money addMonies(int number,
                       int cents,
                       java.lang.Object obj)
                throws java.lang.Exception
A method to use for learning about exceptions. This method returns the sum of "this", the object and number*cents.

Parameters:
number - A number between 1 and 10
cents - The number of cents (between 0 and 99)
obj - An object
Throws:
an - IllegalArgumentException if number is not between 1 and 10.
an - IllegalArgumentException if cents is not between 0 and 99.
a - NullPointerException if obj is null.
a - ClassCastException if obj is not a Money object.
java.lang.Exception

compareTo

public int compareTo(java.lang.Object obj)
Compares this Money object with the specified Money object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Specified by:
compareTo in interface java.lang.Comparable
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

dividedBy

public Money dividedBy(int anAmount)
Returns the result of dividing this Money object an integer amount.

Parameters:
anAmount - the integer that is to be divided into this object.
Returns:
a Money object equal to the quotient of this Money object and anAmount.

equals

public boolean equals(java.lang.Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Money object that represents the same amount of money as this object.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object that is compared to this object.
Returns:
true if the objects are equal, false otherwise.

isEqualTo

public boolean isEqualTo(Money anAmount)
Compares this Money object against the specified Money object. The result is true if and only if the two objects are equal.

Parameters:
anAmount - the Money object that is to be compared to this object.
Returns:
true if the objects are equal, false otherwise.

minus

public Money minus(Money anAmount)
Returns the difference of this Money object and the specified amount of money.

Parameters:
anAmount - the Money object that is to be subtracted from this object.
Returns:
a Money object equal to the result of subtracting anAmount from this Money object.

multipliedBy

public Money multipliedBy(int anAmount)
Returns the result of multiplying this Money object an integer amount.

Parameters:
anAmount - the integer that is to be multiplied by this object.
Returns:
a Money object equal to the product of this Money object and anAmount.

plus

public Money plus(Money anAmount)
Returns the sum of this Money object and the specified amount of money.

Parameters:
anAmount - the Money object that is to be added to this object.
Returns:
a Money object equal to the sum of this Money object and anAmount.

toString

public java.lang.String toString()
Returns a String representation of this Money object in the form: "$ddd.cc" or "-$ddd.cc", where ddd is the dollars part and cc is the cents part.

Overrides:
toString in class java.lang.Object
Returns:
a String representation of this Money object.